﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Microsoft.Win32;
using AgentObjects;
using SimpleUtils;
using SimpleUtils.Win;

namespace SimpleReminder
{
    public partial class frmReminderAgent : frmBase
    {
        public frmReminderAgent()
        {
            InitializeComponent();

            Agent.InitAgentList(cboAgent);
            cboAgent.Items.Add(new SimpleListItem(Agent.RANDOM));

            DialogResult = DialogResult.Cancel;
        }

        private void EnableDisableAll(bool enabled)
        {
            lstAnimation.Enabled = enabled;
            lstSelected.Enabled = enabled;
            btnSelect.Enabled = enabled;
            btnUnSelect.Enabled = enabled;
            nudLeft.Enabled = enabled;
            nudTop.Enabled = enabled;
        }

        private IAgentCtlCharacterEx _character;
        const string CharacterID = MyEvents.REMINDER_AGENT;
        bool _isRandom = false;

        private void LoadAgent()
        {
            SimpleListItem li = cboAgent.SelectedItem as SimpleListItem;
            if (li == null) return;

            lstAnimation.Items.Clear();

            string path = li.Value;
            _isRandom = (path == Agent.RANDOM);

            if (_isRandom)
            {
                //path = Agent.GetRandomAgent();
                EnableDisableAll(false);
            }
            else
            {
                EnableDisableAll(true);
            }

            UnloadCharacter();

            if (path == null || !File.Exists(path) || _isRandom)
                return;

            try
            {
                axAgent.Characters.Load(CharacterID, path);
                _character = axAgent.Characters[CharacterID];
                _character.LanguageID = 0x409;
                
                foreach (string aniName in _character.AnimationNames)
                {
                    lstAnimation.Items.Add(aniName);
                }
                lstAnimation.SelectedIndex = 0;
                lstSelected.Items.Clear();

                _character.Top = (short)nudTop.Value;
                _character.Left = (short)nudLeft.Value;
                _character.Show(null);
                
                //_character.MoveTo((short)nudTop.Value, (short)nudLeft.Value, null);

                if (_character.Balloon.Enabled)
                {
                    _character.Balloon.FontName = "Tahoma";
                    _character.Balloon.FontSize = 10;
                }

                //_agent.Commands.RemoveAll();
                //_agent.Commands.Add("Options",
                //  "&Options", null, null, null);

                SetAgentPosition();
                Agent.SetDefaultBallonStyle(_character);
            }
            catch (Exception ex)
            {
                SimpleMessage.ShowException(ex);
            }
        }

        bool _settingDisplayPosition = false;
        private void SetDisplayPosition()
        {
            try
            {
                _settingDisplayPosition = true;
                if (_character != null)
                {
                    nudTop.Value = _character.Top;
                    nudLeft.Value = _character.Left;
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                _settingDisplayPosition = false;
            }
        }

        private void SetAgentPosition()
        {
            if (_settingDisplayPosition) return;

            if (_character != null)
            {
                _character.Left = (short)nudLeft.Value;
                _character.Top = (short)nudTop.Value;
            }
        }

        private void axAgent_Command(object sender, AxAgentObjects._AgentEvents_CommandEvent e)
        {
            //IAgentCtlUserInput ui = (IAgentCtlUserInput)e.userInput;
            //if (ui.Name == "Options")
            //{
            //    axAgent.PropertySheet.Visible = true;
            //}
        }


        private void UnloadCharacter()
        {
            StopCharacter();
            try
            {
                axAgent.Characters.Unload(CharacterID);
                //Application.DoEvents();
            }
            catch { }
            try
            {
                Program.MainForm.CloseAgent(CharacterID);
            }
            catch { }
        }

        private void frmDemo_FormClosing(object sender, FormClosingEventArgs e)
        {
           UnloadCharacter();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (_isRandom)
            {
                if (Program.MainForm.CloseAgent(CharacterID))
                {
                    Program.MainForm.RunAgent(CharacterID, Agent.RANDOM, 0, 0, null, _agentSpeech);
                }
                return;
            }

            if (_character == null) return;
            
            if(_character.Balloon.Visible)
                _character.Balloon.Visible = false;

            for (int i = 0; i < lstSelected.Items.Count; i++)
            {
                lstSelected.SelectedItems.Clear();
                lstSelected.SelectedIndex = i;
                string ani = lstSelected.Items[i] as string;
                Agent.PlayAnimation(_character, ani);
            }

            if (!String.IsNullOrEmpty(_agentSpeech))
            {
                _character.Speak(_agentSpeech, null);
            }
        }

        private void lstAnimation_DoubleClick(object sender, EventArgs e)
        {
            if (lstAnimation.SelectedItem != null)
            {
                lstSelected.Items.Add(lstAnimation.SelectedItem);
            }
        }

        private void cboAgent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboAgent.SelectedIndex >= 0)
                LoadAgent();
        }

        private void axAgent_MoveEvent(object sender, AxAgentObjects._AgentEvents_MoveEvent e)
        {
            SetDisplayPosition();
        }

        private void nudTop_ValueChanged(object sender, EventArgs e)
        {
            SetAgentPosition();
        }

        private void nudLeft_ValueChanged(object sender, EventArgs e)
        {
            SetAgentPosition();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            StopCharacter();
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            StopCharacter();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void lstSelected_DoubleClick(object sender, EventArgs e)
        {
            if (lstSelected.SelectedItem != null)
            {
                lstSelected.Items.RemoveAt(lstSelected.SelectedIndex);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lstAnimation.SelectedItems.Count; i++)
            {
                lstSelected.Items.Add(lstAnimation.SelectedItems[i]);
            }
        }

        private void btnUnSelect_Click(object sender, EventArgs e)
        {
            while (lstSelected.SelectedItems.Count > 0)
            {
                lstSelected.Items.Remove(lstSelected.SelectedItems[0]);
            }
        }

        public string AgentPath
        {
            get
            {
                SimpleListItem li = cboAgent.SelectedItem as SimpleListItem;
                if (li == null) return null;
                return li.Value;                 
            }
            set
            {
                for (int i = 0; i < cboAgent.Items.Count; i++)
                {
                    SimpleListItem li = cboAgent.Items[i] as SimpleListItem;
                    if (li.Value == value)
                    {
                        cboAgent.SelectedIndex = i;
                        break;
                    }                    
                }
            }
        }

        public short AgentTop
        {
            get { return (short)nudTop.Value; }
            set { nudTop.Value = value; }
        }

        public short AgentLeft
        {
            get { return (short)nudLeft.Value; }
            set { nudLeft.Value = value; }
        }

        public string AgentAnimation
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < lstSelected.Items.Count; i++)
                {
                    sb.AppendFormat("{0};", lstSelected.Items[i] as string);
                }
                return sb.ToString();
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    lstSelected.Items.Clear();
                }
                else
                {
                    string[] animations = value.Split(new char[] { ';' });
                    for (int i = 0; i < animations.Length; i++)
                    {
                        if (String.IsNullOrEmpty(animations[i])) continue;
                        lstSelected.Items.Add(animations[i]);
                    }
                }
            }
        }

        private string _agentSpeech = null;
        public string AgentSpeech
        {
            get { return _agentSpeech; }
            set { _agentSpeech = value; }
        }

        private void StopCharacter()
        {
            Agent.StopCharacter(_character);
            Agent.HideCharacter(_character);
            _character = null;
        }
    }//end of class
}
